$(function(){
/* Ajax loading manager */
	$('body').ajaxStart(function(){
		$('#loading').html('<img src="/Symfony2/web/images/iconLoadingBig.gif" />');
	});
	
	$('body').ajaxStop(function(){
		$('#loading').html('');
	});

/* Functions for leftMenu ********************************************************************/
	$('#leftMenu li').bind('click', function(){
		$('#leftMenu li').removeClass('current');
		$(this).addClass('current');
	});
});

/* Functions for Announcement ****************************************************************/
/* Get the Announcement segment */
function showAnnouncementManager() {
	var url = ANNOUNCEMENT_URL + '/';
	$.post(url, function(res){
		$('#layoutContent').html(res);
	});
	
	return true;
}

function searchAnnouncement(e) {
	if(e && e.which != 13) return false;
	
	/* Get params */
	var id = $('#id').val();
	
	var url = ANNOUNCEMENT_URL + '/search';
	$.post(url, {'id': id}, function(res){
		$('#announcementContentPanel').html(res);
	});
	
	return true;
}

function validateAnnouncement(id) {
	var url = ANNOUNCEMENT_URL + '/validate';
	
	$.post(url, {'id': id}, function(res){
		if (res == 'KO') {
			alert('Error');
			return false;
			
		} else if($('#validate_' + id).html() == 'V'){
			$('#validate_' + id).html('-');
			$('#validatedAt_' + id).html('-');
			
		} else {
			$('#validate_' + id).html('V');
			$('#validatedAt_' + id).html('refesh...');
		}
	});
	
	return true;
}

function deleteAnnouncement(id) {
	if (!confirm('Are you sure ?')) return false;

	var url = ANNOUNCEMENT_URL + '/delete';
	
	$.post(url, {'id': id}, function(res){
		if (res == 'KO') {
			alert('Error');
			return false;
			
		} else {
			$('#announcement_' + id).remove();
		}
	});
	
	return true;
}

/* Functions for Template ****************************************************************/
/* Get the template segment */
function showTemplateManager() {
	var url = TEMPLATE_URL + '/';
	$.post(url, function(res){
		$('#layoutContent').html(res);
	});
	
	return true;
}

/* Get the template edit form */
function editTemplate() {
	var filePath = $('#templateFile').val();

	if (filePath == '') {
		$('#templateContentPanel').html('');
		 return false;
	}
	
	var url = TEMPLATE_URL + '/edit';
	$.post(url, {'filePath': filePath}, function(res){
		$('#templateContentPanel').html(res);
	});
	
	return true;
};

/* Update template */
function updateTemplate() {
	var filePath = $('#templateFile').val();

	if (filePath == '') {
		$('#templateContentPanel').html('Unable to update template <b>' + fileName + '</b>.');
		 return false;
	}

	var fileName = $('#templateFile option:selected').text();
	var fileContent = $('#templateContent').val();
	
	if (fileContent == '') {
		$('#templateFile').val('');
		$('#templateContentPanel').html('Attention: Template <b>' + fileName + '</b> is empty.');
		 return false;
	}
	
	var url = TEMPLATE_URL + '/update';
	$.post(url, {'filePath': filePath, 'fileContent': fileContent}, function(res){
		if (res == 'KO') {
			$('#templateFile').val('');
			$('#templateContentPanel').html('Unable to update template <b>' + fileName + '</b>.');
		} else if (res == 'OK') {
			$('#templateFile').val('');
			$('#templateContentPanel').html('Template <b>' + fileName + '</b> has been updated.');
		}
	});
	
	return true;
};

/* Functions for Language ****************************************************************/
/* Get the language segment */
function showLanguageManager() {
	var url = LANGUAGE_URL + '/';
	$.post(url, function(res){
		$('#layoutContent').html(res);
	});
	
	return true;
}

/* Get the language edit form */
function editLanguage() {
	var filePath = $('#languageFile').val();

	if (filePath == '') {
		$('#languageContentPanel').html('');
		 return false;
	}
	
	var url = LANGUAGE_URL + '/edit';
	$.post(url, {'filePath': filePath}, function(res){
		$('#languageContentPanel').html(res);
	});
	
	return true;
};

/* Update language */
function updateLanguage() {
	var filePath = $('#languageFile').val();

	if (filePath == '') {
		$('#languageContentPanel').html('Unable to update language <b>' + fileName + '</b>.');
		 return false;
	}

	var fileName = $('#languageFile option:selected').text();
	var fileContent = $('#languageContent').val();
	
	if (fileContent == '') {
		$('#languageFile').val('');
		$('#languageContentPanel').html('Attention: Language <b>' + fileName + '</b> is empty.');
		 return false;
	}
	
	var url = LANGUAGE_URL + '/update';
	$.post(url, {'filePath': filePath, 'fileContent': fileContent}, function(res){
		if (res == 'KO') {
			$('#languageFile').val('');
			$('#languageContentPanel').html('Unable to update language <b>' + fileName + '</b>.');
		} else if (res == 'OK') {
			$('#languageFile').val('');
			$('#languageContentPanel').html('Language <b>' + fileName + '</b> has been updated.');
		}
	});
	
	return true;
};

/* Functions for System ****************************************************************/
function showSystemManager() {
	var url = SYSTEM_URL + '/';
	$.post(url, function(res){
		$('#layoutContent').html(res);
	});
	
	return true;
}

function executeSystemAction() {
	var systemAction = $('#systemAction').val();

	if (systemAction == '') {
		$('#systemContentPanel').html('');
		 return false;
	}
	
	/* Flush cache */
	if(systemAction == 'flush_cache') {
		var url = SYSTEM_URL + '/flush_cache';
		var actionName = $('#systemAction option:selected').text();
		
		$.post(url, function(res){
			if (res == 'KO') {
				$('#systemAction').val('');
				$('#systemContentPanel').html('Unable to execute action <b>' + actionName + '</b>. Please check the cache path in PROD.');
			} else if (res == 'OK') {
				$('#systemAction').val('');
				$('#systemContentPanel').html('Execute action <b>' + actionName + '</b> successfully.');
			}
		});
	
	/* Modify parametter && setting*/
	} else if(systemAction == 'modif_config_parametter' || systemAction == 'modif_config_setting') {
		var url = SYSTEM_URL + '/modif_config';
		
		switch(systemAction) {
			case 'modif_config_parametter':
				var filePath = SYMFONY_APP_PATH + '/parameters.ini';
				break;
			case 'modif_config_setting':
				var filePath = SYMFONY_APP_PATH + '/settings.ini';
				break;
		}
		
		$.post(url, {'filePath': filePath} ,function(res){
			$('#systemContentPanel').html(res);
		});

	}
}